package com.cy.sys.serviceImpl;

import com.cy.common.annotation.CleanCache;
import com.cy.common.annotation.RequiredCache;
import com.cy.common.annotation.RequiredLog;
import com.cy.common.excetion.ServiceException;

import com.cy.sys.dao.SysMenuDao;
import com.cy.sys.pojo.RoleMenus;
import com.cy.sys.pojo.Sysmenu;
import com.cy.sys.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private SysMenuDao dao;
    @Override
    @RequiredCache
    public List<Map<Sysmenu,Object>> dofindMenuAll() {


        return dao.dofindAll();
    }

    @Override
    @RequiredLog(operation = "删除菜单")
    @CleanCache
    public int doDeleteObject(Integer id) {
        System.out.println(id);
        if(id==null || id<0)
            throw new IllegalArgumentException("请选中删除项");

        int a=dao.doFindById(id);
        if(a!=0)
            throw new ServiceException("子菜单未删除，删除失败");

        int b=dao.doDeleteObject(id);
        if (b==0)
            throw new ServiceException("方法可能已经不存在");


        return a;
    }

    @Override
    @CleanCache
    public int insertObject(RoleMenus roleMenus) {
        int a=dao.insertObject(roleMenus);
        if (a==0)
            throw new IllegalArgumentException("添加失败");

        return a;
    }

    @Override
    @CleanCache
    public int doUpdateObject(RoleMenus roleMenus) {
        int a=dao.doUpdateObject(roleMenus);
        if(a==0)
            throw new IllegalArgumentException("添加失败，数据库已存在");
        return a;
    }
}
